/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.engines
{
	import com.competentum.events.EWTParserCommandEvent;
	import com.competentum.events.EventReasons;
	import com.competentum.events.WorkloadEvent;
	import com.competentum.lib.engines.AbstractEngine;
	import com.competentum.lib.events.CommandEvent;
	import com.competentum.lib.events.ParserEngineCommandEvent;
	import com.competentum.popups.PreferencesPopup;
	import com.competentum.ui.AlertWithShowingFlag;
	import com.competentum.ui.Messages;
	import com.competentum.ui.Shortcuts;
	import com.competentum.windows.ScopesManager;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;

	/**
	 * Handles commands for keyboard
	 * 
	 * @author Osipov Sergey, Valentine Abanosimov
	 */	
	public class ShortCutsEngine extends AbstractEngine
	{
		public static const TYPE:String = 'ShortCutsEngine';
		
		public function ShortCutsEngine()
		{
			super(TYPE);
		}
		
		/**
		 * Used for obtaining stage
		 * */
		private var _window:DisplayObject;
		/**
		 * listens to stage keyboard events
		 */		
		public function set window(value:DisplayObject):void
		{
			_window = value;
			if (_window.stage)
			{
				addedToStage();
			}
			else
			{
				_window.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			}
		}
		
		private function addedToStage(event:Event = null):void
		{
			_window.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		/**
		 * dispatch commands as UI do depending on key combination 
		 * @param event
		 * 
		 */		
		private function onKeyDown(event:KeyboardEvent):void
		{
			Shortcuts.invokeActionsByKeyboardEvent(event);
		}
		
		private function showSelectFileDialog(event:CloseEvent = null):void
		{
			AlertWithShowingFlag.isShown = false;
			if (event == null || event.detail == Alert.YES)
			{
				var pEvent:EWTParserCommandEvent = new EWTParserCommandEvent(CommandEvent.ACTIVATE_ENGINE);
				pEvent.commandType = InputOutputEngine.TYPE;
				pEvent.reason = ParserEngineCommandEvent.START_READING;
				pEvent.scope = ScopesManager.currentScope;
				dispatchEvent(pEvent);
			}
		}
		
		override protected function activate(event:CommandEvent=null):void
		{
			super.activate(event);
			
			var wEvent:WorkloadEvent = event as WorkloadEvent;
			if (wEvent)
			{
				if (wEvent.reason == EventReasons.LOAD_FILE && !AlertWithShowingFlag.isShown)
				{
					if (ScopesManager.currentScope.isFileChanged)
					{
						AlertWithShowingFlag.show(Messages.LOST_CHANGES_MESSAGE, Messages.WARNING, Alert.YES | Alert.NO, ScopesManager.currentWindow, showSelectFileDialog);
					}
					else
					{
						showSelectFileDialog();
					}
				}
				
				if (wEvent.reason == EventReasons.UNDO)
				{
					ScopesManager.currentScope.historyManager.undo();
				}
				if (wEvent.reason == EventReasons.REDO)
				{
					ScopesManager.currentScope.historyManager.redo();
				}
				
				if (wEvent.reason == EventReasons.SHOW_PREFERENCES)
				{
					var popup:IFlexDisplayObject = PopUpManager.createPopUp(ScopesManager.currentWindow, PreferencesPopup, true);
					PopUpManager.centerPopUp(popup);
				}
			}
		}
		
	}
}